package dao;

import model.Teacher;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TeacherDao {

    /**
     * 教师登录验证
     * 根据教师ID和密码查询数据库，验证身份
     * @param teacherId 教师ID
     * @param password  密码
     * @return Teacher对象，登录成功返回对应教师信息，失败返回null
     */
    public Teacher login(String teacherId, String password) {
        String sql = "SELECT * FROM teachers WHERE teacher_id = ? AND password = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            // 设置SQL参数，防止SQL注入
            stmt.setString(1, teacherId);
            stmt.setString(2, password);

            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                // 登录成功，封装Teacher对象返回
                return new Teacher(
                        rs.getString("teacher_id"),
                        rs.getString("name"),
                        rs.getString("password")
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 登录失败返回null
        return null;
    }
}
